//数组
#ifndef __ARRAY___HPP
#define __ARRAY___HPP

#include"../Universal.h"

namespace NgSiuKei {

template<typename TheClass>
class Array {
    public:
    Array(void);
    Array(const U_L_L &size);
    Array(const Array &array);
    ~Array(void);

    public:
    //=
    Array &operator=(const Array &array);
    //元素获取
    const TheClass &at(const U_L_L &index) const;
    const TheClass &operator[](const U_L_L &index) const;
    //元素赋值
    TheClass &operator[](const U_L_L &index);
    //数组大小
    U_L_L getSize(void) const;
    //打印
    void print(void);

    private:
    TheClass *p;
    U_L_L size;
};

template<class TheClass>
Array<TheClass>::Array(void) {
    this->p = new TheClass[MAX_SIZE];
    this->size = MAX_SIZE;
}

template<class TheClass>
Array<TheClass>::Array(const U_L_L &size) { 
    this->p = new TheClass[size];
    this->size = size;
}

template<class TheClass>
Array<TheClass>::Array(const Array &array) {
    this->size = array.getSize();
    this->p = new TheClass[this->size];
    for(U_L_L i=0; i<this->size; ++i) {
        this->p[i] = array[i];
    }
}

template<class TheClass>
Array<TheClass>::~Array(void) {
    if(nullptr != this->p) {
        delete[] this->p;
        this->p = nullptr;
    }
}

template<class TheClass>
Array<TheClass> &Array<TheClass>::operator=(const Array<TheClass> &array) {
    if(nullptr != &array) {
        delete[] this->p;
        this->size = array.getSize();
        this->p = new TheClass[this->size];
        for(U_L_L i=0; i<this->size; ++i) {
            this->p[i] = array[i];
        }
    }
    return *this;
}

template<class TheClass>
const TheClass &Array<TheClass>::at(const U_L_L &index) const {
    if(index >= this->size) {
        throw EXCEPTION_OUT_OF_BOUNDS;
    }
    return this->p[index];
}

template<class TheClass>
const TheClass &Array<TheClass>::operator[](const U_L_L &index) const {
    if(index >= this->size) {
        throw EXCEPTION_OUT_OF_BOUNDS;
    }
    return this->p[index];
}

template<class TheClass>
TheClass &Array<TheClass>::operator[](const U_L_L &index) {
    if(index >= this->size) {
        throw EXCEPTION_OUT_OF_BOUNDS;
    }
    return this->p[index];
}

template<class TheClass>
U_L_L Array<TheClass>::getSize(void) const {
    return this->size;
}

template<class TheClass>
void Array<TheClass>::print(void) {
    cout << "打印数组: [ ";
    for(U_L_L i=0; i<this->size; ++i) {
        cout << this->p[i] << ' ';
    }
    cout << ']' << endl;
}

}

#endif